package org.fsconnector.spi;

import java.io.File;
import java.io.PrintWriter;
import java.util.Set;

import javax.resource.ResourceException;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;
import javax.resource.spi.ResourceAdapter;
import javax.resource.spi.ResourceAdapterAssociation;
import javax.security.auth.Subject;

import org.apache.log4j.Logger;
import org.fsconnector.cci.FSConnectionFactoryImpl;

public class FSManagedConnectionFactory implements ManagedConnectionFactory,
		ResourceAdapterAssociation {

	private static final long serialVersionUID = 4429607716470168074L;
	private static final Logger logger = Logger.getLogger(FSManagedConnectionFactory.class);

	private ResourceAdapter ra;
	private PrintWriter logWriter;
	private File rootDir = new File("/");

	public Object createConnectionFactory() throws ResourceException {
		return createConnectionFactory(new FSConnectionManager());
	}

	public Object createConnectionFactory(ConnectionManager manager)
			throws ResourceException {
		logger.debug("createConnectionFactory(" + manager + ")");
		return new FSConnectionFactoryImpl(manager, this);
	}
	
	private FSConnectionRequestInfo createDefaultInfo() {
		return new FSConnectionRequestInfo(rootDir);
	}

	public ManagedConnection createManagedConnection(Subject subject,
			ConnectionRequestInfo cxRequestInfo) throws ResourceException {
		logger.debug("createManagedConnection(" + subject + ", " + cxRequestInfo + ")");
		FSConnectionRequestInfo info = (FSConnectionRequestInfo)cxRequestInfo;
		if(info == null) {
			info = createDefaultInfo();
		} else {
			if(info.getRootDir() == null) info.setRootDir(rootDir);
		}
		return new FSManagedConnection(subject, info);
	}

	public PrintWriter getLogWriter() throws ResourceException {
		return logWriter;
	}

	@SuppressWarnings("unchecked")
	public ManagedConnection matchManagedConnections(Set connectionSet, Subject subject,
			ConnectionRequestInfo cxRequestInfo) throws ResourceException {
		logger.debug("matchManagedConnections(" + connectionSet + ", " + subject + ", " + cxRequestInfo + ")");
//		FSConnectionRequestInfo info;
//		if(cxRequestInfo == null) info = createDefaultInfo();
//		else info = (FSConnectionRequestInfo)cxRequestInfo;
//		Iterator<FSManagedConnection> it;
//		for(it = connectionSet.iterator() ; it.hasNext() ;) {
//			FSManagedConnection conn = it.next();
//			logger.debug(conn.getInfo() + " == " + info);
//			if(conn.getInfo().equals(info)) return conn;
//		}
//		return null;
		return (ManagedConnection)connectionSet.iterator().next();
	}

	public void setLogWriter(PrintWriter out) throws ResourceException {
		this.logWriter = out;
	}

	public ResourceAdapter getResourceAdapter() {
		return ra;
	}

	public void setResourceAdapter(ResourceAdapter ra) throws ResourceException {
		this.ra = ra;
	}

	public String getRootDir() {
		return rootDir == null ? "/" : rootDir.getAbsolutePath();
	}

	public void setRootDir(String rootDir) {
		String val = rootDir == null ? "" : rootDir.trim();
		this.rootDir = new File(val.equals("") ? "/" : val);
	}

}
